package aes.server;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * This class represents manager used in order to check exams
 *
 */
public class ExamCheckingManager {

	/**
	 * Retrieves status of exam execution
	 * @param code Exam execution code
	 * @param teacherId Teacher Id
	 * @return Status of the exam execution
	 */
	public static int getStatusOfExecution(String code, int teacherId){
		DBManager db = DBManager.getInstance();	
		String sql="SELECT status "+
				"FROM aes.exam_execution "+ 
				"WHERE code=? AND execTeacherId=?;";
		try {
			PreparedStatement stmt = db.getConnection().prepareStatement(sql);
			stmt.setString(1, code); 
			stmt.setInt(2, teacherId);
			ResultSet rs1 = stmt.executeQuery();
			int res;
			if (!rs1.next())
				res = -1;
			else 
				res = rs1.getInt("status");
			rs1.close();
			return res;
		} catch (SQLException e) {
			e.printStackTrace();
			return-2;
		}
	}

	/**
	 * Confirm student's grade
	 * @param executionCode Exam execution code
	 * @param studentId Student's Id
	 * @param grade Grade
	 * @param notes Notes
	 * @return Confirm result
	 */
	public static boolean confirmStudentGrade(String executionCode, int studentId, int grade, String notes) {
		DBManager db = DBManager.getInstance();	
		String sql="UPDATE aes.student_exam "+
				"SET isApproved=1, grade=?, notes=? "+ 
				"WHERE studentId=? AND examCode=?;";
		try {
			PreparedStatement stmt = db.getConnection().prepareStatement(sql);
			stmt.setInt(1, grade); 
			stmt.setString(2, notes);
			stmt.setInt(3, studentId); 
			stmt.setString(4, executionCode);
			return stmt.executeUpdate() > 0;

		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
}
